package com.baway.biliclitent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baway.biliclitent.R;
import com.baway.biliclitent.model.LiveBean;
import com.baway.biliclitent.util.GlideImageLoader;
import com.baway.biliclitent.view.LiveGridView;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.List;

/**
 * 1.类的用途：
 * 2.作者：${赵渊} on 2017/3/10 11:12
 */
public class LiveFragRecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<LiveBean.DataBean.PartitionsBean> partitions;
    private final Context context;
    private List<String> imageList;

    public LiveFragRecyAdapter(List<LiveBean.DataBean.PartitionsBean> partitions, Context context, List<String> imageList) {
        this.partitions = partitions;
        this.context = context;
        this.imageList = imageList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = null;
        if(viewType==0){
            view = LayoutInflater.from(context).inflate(R.layout.live_recy_item, parent, false);
            holder = new MyViewHolder(view);
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.live_recy_head,parent,false);
            holder = new MyViewHolder1(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position == 0){
            if(holder instanceof  MyViewHolder1){
                //设置图片加载器
                ((MyViewHolder1) holder).live_head_banner.setImageLoader(new GlideImageLoader());
                //设置图片集合
                ((MyViewHolder1) holder).live_head_banner.setImages(imageList);
                ((MyViewHolder1) holder).live_head_banner.setIndicatorGravity(BannerConfig.RIGHT);
                //banner设置方法全部调用完毕时最后调用
                ((MyViewHolder1) holder).live_head_banner.start();
            }
        }else{
            if (holder instanceof MyViewHolder) {
                ((MyViewHolder) holder).item_live_partition_title.setText(partitions.get(position).getPartition().getName());
                ((MyViewHolder) holder).item_live_partition_count.setText("当前" + partitions.get(position).getPartition().getCount() + "个直播");
                Glide.with(context).load(partitions.get(position).getPartition().getSub_icon().getSrc()).into(((MyViewHolder) holder).item_live_partition_icon);
                liveFragGridViewAdapter adapter = new liveFragGridViewAdapter(context, partitions.get(position).getLives());
                ((MyViewHolder) holder).live_grid.setVerticalSpacing(20);
                ((MyViewHolder) holder).live_grid.setHorizontalSpacing(20);
                ((MyViewHolder) holder).live_grid.setAdapter(adapter);
            }
        }
    }

    @Override
    public int getItemCount() {
        return partitions.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return 1;
        }else{
            return 0;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView item_live_partition_title;
        private final TextView item_live_partition_count;
        private final ImageView item_live_partition_icon;
        private final LiveGridView live_grid;

        public MyViewHolder(View itemView) {
            super(itemView);
            item_live_partition_title = (TextView) itemView.findViewById(R.id.item_live_partition_title);
            item_live_partition_count = (TextView) itemView.findViewById(R.id.item_live_partition_count);
            item_live_partition_icon = (ImageView) itemView.findViewById(R.id.item_live_partition_icon);
            live_grid = (LiveGridView) itemView.findViewById(R.id.live_grid);
        }
    }
    class MyViewHolder1 extends RecyclerView.ViewHolder{

        private final Banner live_head_banner;

        public MyViewHolder1(View itemView) {
            super(itemView);
            live_head_banner = (Banner) itemView.findViewById(R.id.live_head_banner);
        }
    }
}
